import 'package:beng_beng_utils/utils/log/BBLogUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../BBLogType.dart';

class SheetLogView extends StatefulWidget {
  @override
  _SheetLogViewState createState() => _SheetLogViewState();
}

class _SheetLogViewState extends State<SheetLogView> {
  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
        child: Container(
          height: MediaQuery.of(context).size.height * 0.9,
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(10), topRight: Radius.circular(10))),
          child: Column(
            children: <Widget>[
              ListTile(
                title: Text("日志级别"),
                trailing: DropdownButton(
                  value: BBLogUtils.getInstance().showLevel,
                  items: BBLogType.values.map((BBLogType value) {
                    return DropdownMenuItem(
                      value: value,
                      child: Text(value.name),
                    );
                  }).toList(),
                  onChanged: (value) {
                    setState(() {
                      BBLogUtils.setLogLevel(value as BBLogType);
                    });
                  },
                ),
              ),
              ListTile(
                title: Text("是否打印日志"),
                trailing: CupertinoSwitch(
                  value: BBLogUtils.getInstance().isCacher,
                  onChanged: (value) {
                    setState(() {
                      BBLogUtils.setCacher(value);
                    });
                  },
                ),
              ),
              ListTile(
                title: Text("清空日志"),
                trailing: CupertinoButton(
                  child: Text("清空"),
                  onPressed: () {
                    setState(() {
                      BBLogUtils.clearLog();
                    });
                  },
                ),
              ),
              ListTile(
                title: Text("刷新"),
                trailing: CupertinoButton(
                  child: Text("刷新"),
                  onPressed: () {
                    setState(() {});
                  },
                ),
              ),
              Expanded(
                  child: ListView.builder(
                      itemCount: BBLogUtils.getInstance().logList.length,
                      itemBuilder: (context, index) {
                        BBLogBean bean =
                            BBLogUtils.getInstance().logList[index];
                        return Column(
                          children: [
                            ListTile(
                              title: Text(
                                bean.msg?.toString()??"",
                                style: TextStyle(color: bean.type.color),
                              ),
                              subtitle: Text(
                                bean.tag??"",
                                style: TextStyle(color: Colors.blue),
                              ),
                              trailing: Text(
                                bean.type.name,
                                style: TextStyle(color: bean.type.color),
                              ),
                              leading: Text(
                                bean.from.name,
                                style: TextStyle(color: Colors.blue),
                              ),
                            ),
                            Text(bean.time.toString()),
                            Divider()
                          ],
                        );
                      })),
            ],
          ),
        ));
  }
}
